const mongoose = require('mongoose')

const EmploymentSchema = new mongoose.Schema(
  {
    // 岗位id
    postId: {
      type: String,
      required: true,
      default: function () {
        return this._id.toString()
      }
    },
    // 是否成功案例
    isSuccessCase: {
      type: Number,
      required: true,
      default: 0
    },
    postDate: {
      type: Date,
      required: true,
      get: v => {
        return v.toISOString().split('T')[0]
      } // 格式化输出为YYYY-MM-DD
    }, // 发布时间
    year: {
      type: Number,
      required: true,
      default: function () {
        // 统一处理日期格式
        const dateObj = this.postDate instanceof Date ? this.postDate : new Date(this.postDate)
        return dateObj.getUTCFullYear()
      }
    }, // 年份
    files: { type: mongoose.Schema.Types.ObjectId, ref: 'File', required: true }, // 图片
    companyName: { type: String, required: true, maxlength: 100 }, // 公司名称
    position: { type: String, required: true, maxlength: 50 }, // 职位
    description: { type: String, required: true, maxlength: 1000 }, // 描述
    isFilled: { type: Number, default: 0 }, // 是否已招满 0:未招满 1:已招满
    studentInfoList: [{ type: String }], // 学生信息
    createdAt: { type: Date, default: Date.now } // 创建时间
  },
  { timestamps: true }
)

EmploymentSchema.set('toJSON', { getters: true })
EmploymentSchema.index({ year: 1, postId: 1, isSuccessCase: 1 })

module.exports = mongoose.model('Employment', EmploymentSchema)
